package com.ruoyi.manage.organization.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StrUtil;
import com.ruoyi.common.utils.ObjUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import com.ruoyi.manage.organization.domain.BizOrganizationDict;
import com.ruoyi.manage.organization.domain.vo.BizOrganizationDictVo;
import com.ruoyi.manage.organization.service.IBizOrganizationDictService;
import com.ruoyi.common.utils.poi.ExcelUtil;
/**
 * 金融机构编码信息Controller
 *
 * @author ruoyi
 * @date 2025-09-08
 */
@Api(tags="金融机构编码信息控制器")
@RestController
@RequestMapping("/manage/organizationDict" )
public class BizOrganizationDictController extends BaseController {
    /**
    * 配置表别名 默认空
    */
    private final static String TABLE_PREFIX=StrUtil.initTableAlias(null);
    @Autowired
    private IBizOrganizationDictService bizOrganizationDictService;

    /**
     * 查询金融机构编码信息列表
     */
    @ApiOperation(value = "查询金融机构编码信息",notes = "金融机构编码信息管理")
    @PreAuthorize("@ss.hasPermi('manage:organizationDict:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(BizOrganizationDictVo bizOrganizationDict,QueryWrapper<BizOrganizationDict> queryWrapper) {
        setQueryCondition(bizOrganizationDict,queryWrapper);
        startPage();
        List<BizOrganizationDict> list = bizOrganizationDictService.list(queryWrapper);
        return getDataTable(list);
    }
    private void setQueryCondition(BizOrganizationDictVo bizOrganizationDict, QueryWrapper<BizOrganizationDict> queryWrapper) {
        if(StrUtil.isNotBlank(bizOrganizationDict.getOrgName())){
            queryWrapper.like(TABLE_PREFIX+"org_name" ,bizOrganizationDict.getOrgName());
        }
        if(StrUtil.isNotBlank(bizOrganizationDict.getOrgCode())){
            queryWrapper.eq(TABLE_PREFIX+"org_code" ,bizOrganizationDict.getOrgCode());
        }
        if(StrUtil.isNotBlank(bizOrganizationDict.getOrgType())){
            queryWrapper.eq(TABLE_PREFIX+"org_type" ,bizOrganizationDict.getOrgType());
        }
        if(StrUtil.isNotBlank(bizOrganizationDict.getOrgBusCode())){
            queryWrapper.eq(TABLE_PREFIX+"org_bus_code" ,bizOrganizationDict.getOrgBusCode());
        }
    }

    /**
     * 导出金融机构编码信息列表
     */
    @PreAuthorize("@ss.hasPermi('manage:organizationDict:export')" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, BizOrganizationDictVo bizOrganizationDict,QueryWrapper<BizOrganizationDict> queryWrapper) {
        if (CollUtil.isEmpty( bizOrganizationDict.getIds())){
            setQueryCondition(bizOrganizationDict,queryWrapper);
        }else {
            queryWrapper.in(TABLE_PREFIX+"id",bizOrganizationDict.getIds());
        }
        List<BizOrganizationDict> list = bizOrganizationDictService.list(queryWrapper);
        ExcelUtil<BizOrganizationDict> util = new ExcelUtil<BizOrganizationDict>(BizOrganizationDict. class);
        util.exportExcel(response,list, "organizationDict");
    }

    /**
     * 获取金融机构编码信息详细信息
     */
    @ApiOperation(value = "获取金融机构编码信息",notes = "金融机构编码信息管理")
    @PreAuthorize("@ss.hasPermi('manage:organizationDict:query')" )
    @GetMapping(value = "/{orgName}" )
    public AjaxResult getInfo(@PathVariable("orgName" ) String orgName) {
        return AjaxResult.success(bizOrganizationDictService.queryGetById(orgName));
    }

    /**
     * 新增金融机构编码信息
     */
    @ApiOperation(value = "新增金融机构编码信息",notes = "金融机构编码信息管理")
    @PreAuthorize("@ss.hasPermi('manage:organizationDict:add')" )
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BizOrganizationDict bizOrganizationDict) {
        return toAjax(bizOrganizationDictService.addBizOrganizationDict(bizOrganizationDict) ? 1 : 0);
    }

    /**
     * 修改金融机构编码信息
     */
    @ApiOperation(value = "修改金融机构编码信息",notes = "金融机构编码信息管理")
    @PreAuthorize("@ss.hasPermi('manage:organizationDict:edit')" )
    @PutMapping
    public AjaxResult edit(@RequestBody BizOrganizationDict bizOrganizationDict) {
        return toAjax(bizOrganizationDictService.updateBizOrganizationDict(bizOrganizationDict) ? 1 : 0);
    }

    /**
     * 删除金融机构编码信息
     */
    @ApiOperation(value = "删除金融机构编码信息",notes = "金融机构编码信息管理")
    @PreAuthorize("@ss.hasPermi('manage:organizationDict:remove')" )
    @DeleteMapping("/{orgNames}" )
    public AjaxResult remove(@PathVariable String[] orgNames) {
        return toAjax(bizOrganizationDictService.deleteBizOrganizationDictByOrgNames(orgNames) ? 1 : 0);
    }
}
